<script setup>
import ConfigDialog from '@/views/dialog/ConfigDialog.vue'
import { defineOptions, ref, provide } from 'vue'
import { dbOptions, generatorOptions, sqlPlaceholder } from '@/utils/config'
import { copy2Clipboard } from '@/utils/commonUtil.js'
import { ElMessage } from 'element-plus'
import { useMappingStore } from '@/stores/mapping.js'
import { useConfigStore } from '@/stores/config.js'

const configStore = useConfigStore()
const mappingStore = useMappingStore()
import {
  generateResult,
  generateSearchButtons,
  parseTableInfo,
  convertTableInfo,
  generateJavaCode,
  generateXml,
  generateEntity,
  generateXmlOrHtml,
  generateJs
} from '@/utils/sqlGenerator.js'

defineOptions({
  name: 'SqlGenerator'
})
provide('closeDialog', () => {
  handleDialogClose()
})
const data = ref({
  showConfig: false,
  row: 10,
  leftStr: '',
  rightStr: '',
  dbType: 'mysql',
  fileType: 'Entity',
  tableInfo: {},
  afterConvert: {}
})
const showConfig = ref(false)
const handleDialogClose = () => {
  showConfig.value = false
}
const download = () => {
  // uTools 环境
  // if (window.utools) {
  //   const outputPath = window.services.writeGeneratorFile(data.value.rightStr, downloadFileName())
  //   if (outputPath) {
  //     // 在资源管理器中显示
  //     window.utools.shellShowItemInFolder(outputPath)
  //   }
  // } else {
    // 浏览器环境
    const blob = new Blob([data.value.rightStr], { type: 'text/plain' })
    const link = document.createElement('a')
    link.download = downloadFileName()
    link.href = URL.createObjectURL(blob)
    link.click()
    URL.revokeObjectURL(link.href)
  // }
}
const downloadFileName = () => {
  const className = data.value.afterConvert.table.className
  const firstLowerClassName = data.value.afterConvert.table.firstLowerClassName
  switch (data.value.fileType) {
    case 'Entity':
      return `${ className }.java`
    case 'Mapper':
      return `${ className }Mapper.java`
    case 'Controller':
      return `${ className }Controller.java`
    case 'Service':
      return `${ className }Service.java`
    case 'Impl':
      return `${ className }ServiceImpl.java`
    case 'Xml':
      return `${ className }Mapper.xml`
    case 'MockJs':
      return `${ firstLowerClassName }Mock.js`
    case 'ApiJs':
      return `${ firstLowerClassName }.js`
    case 'IndexVue':
      return `index.vue`
    case 'FormVue':
      return `form.vue`
    default:
      return 'untitled.txt'
  }
}
const generator = () => {
  const fileType = data.value.fileType
  if (fileType === 'Result') {
    generateResult(data.value, configStore.config)
  } else if (fileType === 'SearchButtons') {
    generateSearchButtons(data.value)
  } else {
    parseTableInfo(data.value)
    data.value.afterConvert = convertTableInfo(data.value, configStore.config, mappingStore.mapping[data.value.dbType])
    if (fileType !== '') {
      switch (fileType) {
        case 'Entity':
          generateEntity(data.value, configStore.config)
          break
        case 'Mapper':
        case 'Controller':
        case 'Service':
        case 'Impl':
          generateJavaCode(data.value)
          break
        case 'Xml':
          generateXml(data.value)
          break
        case 'MockJs':
        case 'ApiJs':
          generateJs(data.value)
          break
        case 'IndexVue':
        case 'FormVue':
        case 'IndexVue3':
        case 'FormVue3':
          generateXmlOrHtml(data.value)
          break
        case 'Result':
          generateResult(data.value)
          break
        case 'SearchButtons':
          generateSearchButtons(data.value)
          break
        default:
          data.value.rightStr = '类型错误'
      }
    } else {
      ElMessage.error('请选择生成文件类型')
    }
  }
}
</script>
<template>
  <div class="container">
    <el-input
        type="textarea"
        class="textarea left"
        :row="data.row"
        v-model="data.leftStr"
        :placeholder="sqlPlaceholder"
    />
    <el-input
        type="textarea"
        class="textarea right"
        :row="data.row"
        v-model="data.rightStr"
    />
  </div>
  <el-footer>
    <el-row :gutter="20">
      <el-col :span="4">
        <el-select v-model="data.dbType" placeholder="数据库" filterable>
          <el-option v-for="item in dbOptions" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select
            v-model="data.fileType"
            placeholder="生成类型"
            @change="generator"
            filterable>
          <el-option v-for="item in generatorOptions" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="3">
        <el-button @click="copy2Clipboard(data)" type="primary">
          复制
        </el-button>
      </el-col>
      <el-col :span="3">
        <el-button @click="generator" type="primary">
          生成
        </el-button>
      </el-col>
      <el-col :span="3">
        <el-button @click="download" type="primary">
          下载
        </el-button>
      </el-col>
      <el-col :span="3">
        <el-button @click="showConfig = true" type="primary">
          配置
        </el-button>
      </el-col>
    </el-row>
  </el-footer>
  <config-dialog
      v-model="showConfig"
      @handle-dialog-close="handleDialogClose"
  />
</template>
<style></style>
